package board;

import java.io.Reader;
import java.sql.SQLException;
import java.util.List;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class BoardDao {

	private static SqlMapClient sqlMapper;
	private static BoardDao dao = new BoardDao();
	
	public static BoardDao getInstance(){
		try {
			Reader reader = Resources.getResourceAsReader("kosta/ibatis/SqlMapConfig.xml");
			sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return dao;
	}  
	
	//WRITE;
	public void insertBoard(BoardData board) throws SQLException{
		sqlMapper.insert("insertBoard", board);
	}

	//LIST;
	public List<BoardData> getBoardList() throws SQLException{
		return sqlMapper.queryForList("getBoardList");
	}
	
	//SEARCH;
	public List<BoardData> searchBoard(Search search) throws SQLException{
		return sqlMapper.queryForList("searchBoard", search);
	}
	
	//DETAIL;
	public BoardData getBoardDetail(int board_id) throws SQLException{
		sqlMapper.update("updateHit", board_id);
		return (BoardData) sqlMapper.queryForObject("getBoardDetail", board_id);
	}
	
	//UPDATE;
	public void updateBoard(BoardData board) throws SQLException{
		sqlMapper.update("updateBoard", board);
	}
	
	//DELETE;
	public void deleteBoard(int board_id) throws SQLException{
		sqlMapper.delete("deleteBoard", board_id);
		sqlMapper.update("deleteBoardAS", board_id);
	}
	
}
